﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace AGuang.Script {
    /// <summary>
    /// 此项目为脚本包，由于每做项目都将脚本复制过去，并且有所更新，最后导至不知道那里的脚本为最新版本。现将脚本放入脚本库中，统一引用。
    /// (基础包)，只需基本功能时引入该包
    /// </summary>
    public class BaseScript:Control {
        private StringBuilder sBuilder = new StringBuilder();
        private Control eCon = null;

        /// <summary>
        /// 加载script
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e) {
            ClientScriptManager csm = Page.ClientScript;
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.expand.js"));
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.EventOperate.js"));
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.Document.js"));
        }

        /// <summary>
        /// 将脚本加入页面
        /// </summary>
        /// <param name="url">脚本链接</param>
        protected void AddScript(String url) {
            ControlCollection cols = FindControls();
            //不添加已存在的脚本
            foreach (Control oc in cols) {
                if (oc is HtmlGenericControl) {
                    HtmlGenericControl hgc = (HtmlGenericControl)oc;
                    if (hgc.TagName.ToLower() == "script") {
                        if (hgc.Attributes["src"] == url) return;
                    }
                }
            }
            HtmlTextWriter writer = new HtmlTextWriter(System.IO.TextWriter.Null);
            HtmlGenericControl c = new HtmlGenericControl("script");
            c.Attributes["src"] = url;
            c.Attributes["type"] = "text/javascript";
            c.Attributes["charset"] = "utf-8";
            cols.Add(c);
        }

        /// <summary>
        /// 将css样式加入页面
        /// </summary>
        /// <param name="url">脚本链接</param>
        protected void AddStyle(String url) {
            ControlCollection cols = FindControls();
            foreach (Control oc in cols) {
                if (oc is HtmlGenericControl) {
                    HtmlGenericControl hgc = (HtmlGenericControl)oc;
                    if (hgc.TagName.ToLower() == "link") {
                        if (hgc.Attributes["href"] == url) return;
                    }
                }
            }
            HtmlGenericControl c = new HtmlGenericControl("link");
            c.Attributes["href"] = url;
            c.Attributes["type"] = "text/css";
            c.Attributes["charset"] = "utf-8";
            c.Attributes["rel"] = "Stylesheet";
            cols.Add(c);
        }

        /// <summary>
        /// 添加执行脚本
        /// </summary>
        /// <param name="script"></param>
        protected void AddEffect(String script) {
            sBuilder.Append(script);
        }


        /// <summary>
        /// 写出标记
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer) {
            if (eCon == null) {//如果有控件位置，则在当前标签位置写出js
                //writer.WriteBeginTag("script");
                //writer.WriteAttribute("type", "text/javascript");
                //writer.Write(HtmlTextWriter.TagRightChar);
                //writer.Write(sBuilder);
                //writer.WriteEndTag("script");
                
                HtmlGenericControl c = new HtmlGenericControl("script");
                c.Attributes["type"] = "text/javascript";
                c.InnerHtml = sBuilder.ToString();
                this.Controls.Add(c);
                base.Render(writer);
            }
        }

        /// <summary>
        /// 添加脚本控件
        /// </summary>
        /// <param name="c"></param>
        protected void AddScriptContorl(BaseScript c) {
            ControlCollection cols = FindControls();
            cols.Add(c);
        }

        protected ControlCollection FindControls() {
            ControlCollection cols = null;
            if (Page.Header != null) cols = Page.Header.Controls;
            else if (Page.Form != null) cols = Page.Form.Controls;
            else cols = this.Controls;
            return cols;
        }
    }
}
